package com.example.redismanual.dao.actionExamples.ZsetExamples;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

@Component
public class AutoComplete {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    public void feed(String content,int weight){
        for (int i = 0; i < content.length(); i++) {
            String key = "autocomple::" + content.substring(0,i+1);
            stringRedisTemplate.opsForZSet().incrementScore(key,content,weight);
        }
    }

    public Set<String> hint(String prefix,int count){
        String key = "autocomple::" + prefix;
        return stringRedisTemplate.opsForZSet().reverseRange(key,0,count-1);
    }

}
